package com.obor.quartz.job;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

import javax.annotation.PostConstruct;

import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.Trigger.TriggerState;
import org.quartz.impl.matchers.EverythingMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.zn.commons.collection.Maps;

import com.obor.quartz.job.service.MongoService;

/**
* 
* 
* @Company OBOR
* @Author 周楠
* @Description
* 2019年4月3日
*  Job 状态
*
*
*/
@Component
public class JobInspector implements JobListener {
	
	@Autowired
	private Scheduler scheduler;
	@Autowired
	private MongoService mongoService;
	
	public MongoService getMongoService() {
		return mongoService;
	}


	public void setMongoService(MongoService mongoService) {
		this.mongoService = mongoService;
	}
	public Scheduler getScheduler() {
		return scheduler;
	}

	public void setScheduler(Scheduler scheduler) {
		this.scheduler = scheduler;
	}



	@PostConstruct
	public void init() {
		
		 try {
			scheduler.getListenerManager().addJobListener(this, EverythingMatcher.allJobs());
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "ALL";
	}
	
	
	private JobInfo getJobInfo(JobExecutionContext context) {
		
		JobDetail jd= context.getJobDetail();
		Trigger tr= context.getTrigger();
		JobInfo ji=new JobInfo();
		
		ji.setKey(jd.getKey().getName());
		ji.setGroup(jd.getKey().getGroup());
		
	  ji=mongoService.findJobStatus(ji);
	 return ji;
		
	}
	
	

	@Override
	public void jobToBeExecuted(JobExecutionContext context) {
		// TODO Auto-generated method stub
		
		
		JobInfo ji=getJobInfo(context);
		
		Map<String, Object> map=Maps.newMap();
		
		
		map.put("name", ji.getName());
		map.put("key", ji.getKey());
		map.put("group", ji.getGroup());
		map.put("logTime", new Date());
		map.put("status", "To Be Executed");
		
		
		mongoService.insertLogs(map);
		
	}

	@Override
	public void jobExecutionVetoed(JobExecutionContext context) {
		// TODO Auto-generated method stub
		
		
		
		
	}

	@Override
	public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
		// TODO Auto-generated method stub
		
		JobDetail jd= context.getJobDetail();
		Trigger tr= context.getTrigger();
		
		try {
			Date date = scheduler.getTriggersOfJob(jd.getKey()).get(0).getNextFireTime();
	
			TriggerState ts=  scheduler.getTriggerState(context.getTrigger().getKey());
			
			System.out.println( date);
			System.out.println( ts.name());
			
			String status="";
			
			if(Objects.isNull(jobException)) {
				
				status="exception";
			}else {
				
				status=ts.name();
				
			}
			
			
			
			JobInfo ji=getJobInfo(context);
			
			ji.setStatus(status);
			ji.setUpdateTime(new Date());
			
			Map<String, Object> map=Maps.newMap();
			
			map.put("name", ji.getName());
			map.put("key", ji.getKey());
			map.put("group", ji.getGroup());
			map.put("logTime", new Date());
			map.put("status", ji.getStatus());
			
			mongoService.insertLogs(map);
			mongoService.jobStatsUpdate(ji);
		
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		

		
	}
	
	
	

}
